"use strict"
var items = new Array();
$(document).ready(function () {

    var item = {

        constructor: function (category, sub, name, id, price_fp, price_hp) 
		{

            this.category = category;
            this.subcategory = sub;
            this.name = name;
            this.id = id;
            this.price_fp = price_fp;
            this.price_hp = price_hp;

        },

        //Working method..populates items array w/ data from JSON. because this is called asynchronously
        //execution order matters. hence encapsulating within function

        getdata: function () 
		{
			var i = 0;
            $.getJSON('menu.json', function (data) {

                $.each(data, function (index, d) {
                    //	alert(d.restaurant_name); // restaurant name
                    $.each(d.category, function (index, e) {
                        //		alert(e.name);  //category name
                        if (e.category_sub) {
                            $.each(e.category_sub, function (index, f) {
                                //				alert(f.name);  //sub-category
                                $.each(f.item, function (index, g) {
                                    //				alert(g.name);  // item-name
                                    items[i] = new item.constructor(e.name, f.name, g.name, g.id, g.price_fp, g.price_hp);
                               //     alert(items.length);
                                    i++;
                                });
                            });
                        } else {
                            $.each(e.item, function (index, h) {
                                //			alert(h.name);  //item-name
                                items[i] = new item.constructor(e.name, null, h.name, h.id, h.price_fp, h.price_hp);
                          //      alert(items[i].name);
                                i++;
                          //      alert(items.length);
                            });
                        }

                    });
                });

                item.init();

            });
        },

        init: function () 
		{

            alert("length outside" + items.length);
			demo.test_fn();
			var datastore = [];
			var i = 0;
			$.each(items,function(index,data){
			
				datastore[i++] = data.id + "->" + data.category + (data.subcategory?"->"+data.subcategory:"") + "->" + data.name;
			});
			$( "#autocomplete" ).autocomplete({
			source: datastore
});

        }
    };
	
	item.getdata();
	




});